package com.dankegongyu.component.jsbridge;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.dankegongyu.component.jsbridge.bean.DKWebFinishEvent;
import com.dankegongyu.component.jsbridge.config.DKWebConfig;
import com.dankegongyu.component.jsbridge.config.DKWebService;
import com.dankegongyu.component.jsbridge.config.DKWebServiceManager;
import com.dankegongyu.component.jsbridge.config.DKWebToolbarConfig;
import com.dankegongyu.component.jsbridge.utils.JsUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * h5
 */
public class WebActivity extends AppCompatActivity {

    private String mServiceKey;

    /**
     * WebFragment
     */
    private WebFragment webFragment;

    private DKWebService mService;
    private DKWebConfig mConfig;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        if (intent != null) {
            mServiceKey = intent.getStringExtra(JsBridgeConstants.Router.ROUTER_PARAM_WEB_SERVICE_KEY);
        }

        setContentView(R.layout.jbp_activity_web);

        setStatusbar();

        webFragment = WebFragment.newInstance(mServiceKey);

        JsUtil.addFragmentToActivity(getSupportFragmentManager(), webFragment, R.id.container);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (webFragment != null) {
            webFragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onBackPressed() {
        if (webFragment != null && webFragment.onBackPressed()) {
            return;
        }
        finish();
    }

    @Override
    protected void onDestroy() {
        DKWebServiceManager.getInstance().clear(mServiceKey);
        super.onDestroy();
    }

    @Override
    public void finish() {
        EventBus.getDefault().post(new DKWebFinishEvent(mServiceKey));
        super.finish();
        overridePendingTransition(R.anim.jbp_no_move, R.anim.jbp_out);
    }

    private void setStatusbar() {
        mService = DKWebServiceManager.getInstance().get(mServiceKey);
        mService.setContext(this);
        mConfig = mService.getConfig();
        if (mConfig != null && mConfig.getToolbar() != null) {
            DKWebToolbarConfig toolbarConfig = mConfig.getToolbar();
            if (toolbarConfig.getStatusbarColor() > 0) {
                JsUtil.setColor(this, toolbarConfig.getStatusbarColor(), toolbarConfig.getStatusbarAlpha());
            }
            if (toolbarConfig.getStatusbarMode() == JsBridgeConstants.Statusbar.STATUSBAR_DARK) {
                JsUtil.setDarkMode(this);
            } else if (toolbarConfig.getStatusbarMode() == JsBridgeConstants.Statusbar.STATUSBAR_LIGHT) {
                JsUtil.setLightMode(this);
            }
        }
    }

}

